// Wait until the BIS Functions module is initialised.
waituntil {!isnil "bis_fnc_init"};

diag_log "init.sqf startup.";

// Compile/Preprocess scripts for all users...
call compile preprocessfile "Functions\handleShowBaseMarker.sqf";
call compile preprocessfile "Functions\endMission.sqf";
call compile preprocessfile "Functions\showRadioChannel.sqf";
call compile preprocessfile "Functions\setupBriefingsAndEquipment.sqf";
call compile preprocessfile "Functions\hostageDeathCheck.sqf";

DEBUGMISSION= 1;


// Handler for receiving public Variables for the base markers...
// BLUFOR, OPFOR and INDFOR
// These won't be run on the server.
if (isNil "BASEMARKERS") then
{	
	BASEMARKERS=[0, 0, 0];
};


onPlayerConnected "[_id, _name] execVM ""onPlayerConnected.sqf""";

// Some ACE specific settings.
ace_sys_tracking_markers_enabled = false;

// Wait until player is initialised.
if (!isServer) then
{
	diag_log "Waiting for player initialisation";
	waituntil { not isNull player};
};

// Following only runs on the server...
if (isServer) then
{
	diag_log "We are server.";

	// PreProcs - SERVER ONLY.
	call compile preprocessfile "SHK_pos\shk_pos_init.sqf";
	call compile preprocessfile "Functions\locateBase.sqf";

	// determine the starting/end points for each side...
	// Move the starting equipment to each location, and the trigger for winning
	// conditions of the endgame.
	// TODO: Have the bases a fair way away from each other...
	private "_basePos";
    	_basePos= [BLUFOR_SPAWN] call HS_Find_Base_Pos;
	nul= [BLUFOR_START, _basePos, 50] execvm "SHK_moveobjects\SHK_moveobjects.sqf";
	BASEMARKERS set [0, _basePos];

    	_basePos= [OPFOR_SPAWN] call HS_Find_Base_Pos;
	nul= [OPFOR_START, _basePos, 50] execvm "SHK_moveobjects\SHK_moveobjects.sqf";
	BASEMARKERS set [1, _basePos];

	_basePos= [INDFOR_SPAWN] call HS_Find_Base_Pos;
	nul= [INDFOR_START, _basePos, 50] execvm "SHK_moveobjects\SHK_moveobjects.sqf";
	BASEMARKERS set [2, _basePos];

	// Now publicvariable those positions
	publicVariable "BASEMARKERS";

	// Setup the Radio Channel.
	RADIOCHANNEL= Round(Random 100);
	publicVariable "RADIOCHANNEL";

	// Ready to let the client init's do their thing...
	setupDone= true;
	publicVariable "setupDone";

};

// Briefings and Equipment...
call HS_Setup_Briefings;

// Public Variable handler for processing end of game stuff.
"ENDGAME" addPublicVariableEventHandler {[_this select 1] call HS_End_Mission};

if (isDedicated) exitWith {};

// Players only from here on in...we wait until the server init's finished.
diag_log "Client waiting for server to process Init.";
waitUntil{not isNil "setupDone"};
if (setupDone) then 
{
	diag_log "Processing client init settings.";
	
	// By now, the Radio and base positions have been pushed out.
	[BASEMARKERS] call HS_Handle_Show_Base_Markers;
	[RADIOCHANNEL] call HS_Show_Radio_Channel;

	// Other settings for players..
	player setVariable ["ace_sys_wounds_no_medical_gear", true];
	player setVariable ["ace_sys_tracking_markers_enabled", false];

	// Setup the Civilin's kill events.
	// If the Civilian's are killed, then it's mission over...  SPAWN because we want to pause inside the function..if all dead.
	// TODO we should only mission over if both hostages are dead.
	if (!isNil "HOSTAGE1") then {HOSTAGE1 addEventHandler ["killed", {[] spawn HS_Hostage_Death_Check;}];};
	if (!isNil "HOSTAGE2") then {HOSTAGE2 addEventHandler ["killed", {[] spawn HS_Hostage_Death_Check;}];};

	// Loadouts.
	_dummy = [] execVM "lea\loadout.sqf";

	// CTB Operator Menu.
	BIS_MENU_GroupCommunication = 
	[
		["User menu", false],
		["Operator Menu", [2], "", -5, [["expression",  "[] execVM ""CTB_Operator_Menu\CTB_Operator_Menu.sqf"";"]], "1", "1"]
	];

}


